package hendler

import (
	"github.com/gin-gonic/gin"
	"live/dao"
	"live/pkg/jwt"
	"live/pkg/md5"
	"live/request"
	"live/response"
)

func Register(c *gin.Context) {
	var data request.UserAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	where, err := dao.Where(data.Name)
	if where.Id != 0 {
		response.ResponseError(c, "用户已存在", err)
		return
	}
	register, err := dao.Register(data.Name, md5.MD5(data.Password), data.Money)
	if register.Id == 0 {
		response.ResponseError(c, "注册失败", err)
		return
	}
	response.ResponseSuccess(c, "注册成功", register)

}

func Login(c *gin.Context) {
	var data request.UserLogin
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	register, err := dao.Login(data.Name, md5.MD5(data.Password))
	if register.Id == 0 {
		response.ResponseError(c, "用户不存在", err)
		return
	}
	userId := jwt.CustomClaims{ID: uint(register.Id)}
	token, err := jwt.NewJWT("2210A").CreateToken(userId)
	if err != nil {
		response.ResponseError(c, "token生成失败", err)

		return
	}
	response.ResponseSuccess(c, "登录成功", token)
}

func AddRoom(c *gin.Context) {
	var data request.RoomAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}

	Room, err := dao.AddRoom(data.Name, data.Brief, data.Describe)
	if Room.Id == 0 {
		response.ResponseError(c, "添加直播间失败", err)
		return
	}
	response.ResponseSuccess(c, "添加直播间成功", Room)
}

func ListRoom(c *gin.Context) {
	var data request.RoomList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room, err := dao.ListRoom(data.Id)
	if room.Id == 0 {
		response.ResponseError(c, "直播间不存在", err)
		return
	}

	response.ResponseSuccess(c, "查询成功", room)
}

func UpdateRoom(c *gin.Context) {
	var data request.RoomUpdate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.UpdateRoom(data.Id, data.Brief)
	if room == false {
		response.ResponseError(c, "直播间修改失败", err)
		return
	}

	response.ResponseSuccess(c, "修改直播间内容成功", room)
}

func DeleteRoom(c *gin.Context) {
	var data request.RoomDelete
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.DeleteRoom(data.Id)
	if room == false {
		response.ResponseError(c, "直播间删除失败", err)
		return
	}

	response.ResponseSuccess(c, "删除直播间成功", room)
}

func AddComment(c *gin.Context) {
	var data request.CommentAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	userId := c.GetUint("userid")
	Room, err := dao.AddComment(userId, data.Comment, data.RoomId)
	if Room.Id == 0 {
		response.ResponseError(c, "添加评论失败", err)
		return
	}
	response.ResponseSuccess(c, "添加评论成功", Room)
}

func ListComment(c *gin.Context) {
	var data request.CommentList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	Comment, err := dao.ListComment(data.RoomId)
	if err != nil {
		response.ResponseError(c, "查询失败", err)
		return
	}

	response.ResponseSuccess(c, "评论查询成功", Comment)
}

func UpdateComment(c *gin.Context) {
	var data request.CommentUpdate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.UpdateComment(data.Id, data.Comment)
	if room == false {
		response.ResponseError(c, "评论修改失败", err)
		return
	}

	response.ResponseSuccess(c, "修改评论内容成功", room)
}

func DeleteComment(c *gin.Context) {
	var data request.CommentDelete
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.DeleteComment(data.Id)
	if room == false {
		response.ResponseError(c, "评论删除失败", err)
		return
	}

	response.ResponseSuccess(c, "删除评论成功", room)
}

func AddGift(c *gin.Context) {
	var data request.ReceiveAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	userId := c.GetUint("userid")
	money, err := dao.Usermoney(userId)
	if money.Id == 0 {
		response.ResponseError(c, "发送礼物用户不存在", err)
		return
	}
	gift, err := dao.Usergift(data.GiftId)
	if gift.Id == 0 {
		response.ResponseError(c, "礼物不存在", err)
		return
	}
	register, err := dao.UserWhere(data.ReceiveId)
	if register.Id == 0 {
		response.ResponseError(c, "接收礼物用户不存在", err)
		return
	}
	if money.Money < int(gift.Money) {
		response.ResponseError(c, "钱不够", err)
		return
	}
	jmoney := register.Money + int(gift.Money)
	fmoney := money.Money - int(gift.Money)
	j := dao.UpdateUser(register.Id, jmoney)
	f := dao.UpdatefUser(register.Id, fmoney)
	if j == false {
		response.ResponseError(c, "修改金钱失败", err)
		return
	}
	if f == false {
		response.ResponseError(c, "修改金钱失败", err)
		return
	}
	Room, err := dao.AddReceive(userId, data.Notes, data.RoomId, data.ReceiveId, data.GiftId)
	if Room.Id == 0 {
		response.ResponseError(c, "发送礼物失败", err)
		return
	}
	response.ResponseSuccess(c, "发送礼物成功", Room)
}

func ListGift(c *gin.Context) {
	var data request.ReceiveList
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	Receive, err := dao.ListReceive(data.RoomId)
	if err != nil {
		response.ResponseError(c, "查询失败", err)
		return
	}

	response.ResponseSuccess(c, "查询成功", Receive)
}

func UpdateGift(c *gin.Context) {
	var data request.ReceiveUpdate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.UpdateReceive(data.Id, data.Notes)
	if room == false {
		response.ResponseError(c, "修改失败", err)
		return
	}

	response.ResponseSuccess(c, "修改内容成功", room)
}

func DeleteGift(c *gin.Context) {
	var data request.CommentDelete
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	room := dao.DeleteReceive(data.Id)
	if room == false {
		response.ResponseError(c, "礼物删除失败", err)
		return
	}

	response.ResponseSuccess(c, "删除礼物成功", room)
}
